import 'package:flutter/material.dart';

//添加服务记录弹出框
class EmployerServerRecordDialog extends StatefulWidget {
  @override
  _EmployerServerRecordDialogState createState() => _EmployerServerRecordDialogState();
}

class _EmployerServerRecordDialogState extends State<EmployerServerRecordDialog> {
  int selectedMethodIndex = -1;
  int selectedIntentIndex = -1;
  final TextEditingController remarkController = TextEditingController();

  @override
  Widget build(BuildContext context) {
    return Material(
      type: MaterialType.transparency,
      child: Center(
        child: Container(
          margin: EdgeInsets.symmetric(horizontal: 20, vertical: 40),
          padding: const EdgeInsets.all(20),
          decoration: BoxDecoration(
            color: Colors.white,
            borderRadius: BorderRadius.circular(12),
          ),
          child: SingleChildScrollView(
            child: Column(
              mainAxisSize: MainAxisSize.min,
              children: [
                // 顶部标题 + 关闭按钮
                Stack(
                  children: [
                    Align(
                      alignment: Alignment.center,
                      child: Text(
                        '沟通记录',
                        style: TextStyle(fontSize: 14, fontWeight: FontWeight.bold),
                      ),
                    ),
                    Align(
                      alignment: Alignment.centerRight,
                      child: GestureDetector(
                        onTap: () => Navigator.of(context).pop(),
                        child: CircleAvatar(
                          radius: 14,
                          backgroundColor: Colors.grey[300],
                          child: Icon(Icons.close, size: 18, color: Colors.black),
                        ),
                      ),
                    ),
                  ],
                ),
                SizedBox(height: 20),
                buildLabel('沟通方式'),
                SizedBox(height: 10),
                buildMethodRow(),
                SizedBox(height: 20),
                buildLabel('雇主意向度'),
                SizedBox(height: 10),
                buildIntentRow(),
                SizedBox(height: 20),

                buildLabel('备注'),
                SizedBox(height: 10),
                Container(
                  height: 100,
                  padding: EdgeInsets.symmetric(horizontal: 10),
                  decoration: BoxDecoration(
                    border: Border.all(color: Colors.grey),
                    borderRadius: BorderRadius.circular(8),
                  ),
                  child: TextField(
                    controller: remarkController,
                    maxLines: null,
                    expands: true,
                    decoration: InputDecoration.collapsed(hintText: '请输入备注...'),
                  ),
                ),
                SizedBox(height: 25),

                SizedBox(
                  width: double.infinity,
                  height: 45,
                  child: ElevatedButton(
                    onPressed: () {
                      // 提交逻辑
                    },
                    style: ElevatedButton.styleFrom(
                      backgroundColor: Colors.red,
                      shape: RoundedRectangleBorder(
                        borderRadius: BorderRadius.circular(30),
                      ),
                      padding: EdgeInsets.symmetric(vertical: 14),
                    ),
                    child: Text(
                      '提交',
                      style: TextStyle(fontSize: 14, color: Colors.white),
                    ),
                  ),
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }

  Widget buildLabel(String text) => Align(
    alignment: Alignment.centerLeft,
    child: Text(
      text,
      style: TextStyle(fontSize: 14, fontWeight: FontWeight.bold),
    ),
  );

  Widget buildMethodRow() {
    return Row(
      children: [
        Expanded(child: buildOptionButton('平台电话', 0, selectedMethodIndex, (i) => selectedMethodIndex = i)),
        Expanded(child: buildOptionButton('微信', 1, selectedMethodIndex, (i) => selectedMethodIndex = i)),
        Expanded(child: buildOptionButton('其他', 2, selectedMethodIndex, (i) => selectedMethodIndex = i)),
      ],
    );
  }

  Widget buildIntentRow() {
    return Row(
      children: [
        Expanded(child: buildOptionButton('高', 0, selectedIntentIndex, (i) => selectedIntentIndex = i, isIntent: true)),
        Expanded(child: buildOptionButton('中', 1, selectedIntentIndex, (i) => selectedIntentIndex = i, isIntent: true)),
        Expanded(child: buildOptionButton('低', 2, selectedIntentIndex, (i) => selectedIntentIndex = i, isIntent: true)),
        Expanded(flex:2,child: buildOptionButton('无效需求', 3, selectedIntentIndex, (i) => selectedIntentIndex = i, isIntent: true)),
      ],
    );
  }

  Widget buildOptionButton(String label, int index, int selectedIndex, Function(int) onTap, {bool isIntent = false}) {
    final selected = selectedIndex == index;
    return GestureDetector(
      onTap: () => setState(() => onTap(index)),
      child: Container(
        height: 30,
        margin: EdgeInsets.symmetric(horizontal: 4),
        decoration: BoxDecoration(
          color: selected ? Colors.red : Colors.grey[300],
          borderRadius: BorderRadius.circular(3),
        ),
        child: Center(
          child: Text(
            label,
            style: TextStyle(
              color: selected ? Colors.white : Colors.black,
              fontSize: 14,
            ),
            textAlign: TextAlign.center,
          ),
        ),
      ),
    );
  }
}
